package com.cloudsigns.factorymanager2.engineering.activity;

import android.content.Intent;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.cloudsigns.factorymanager2.R;
import com.cloudsigns.factorymanager2.engineering.fragment.EngineeringMaterialUseFragment;
import com.cloudsigns.factorymanager2.engineering.fragment.EngineeringOutfieldPurchaseFragment;
import com.cloudsigns.factorymanager2.engineering.fragment.EngineeringReturnLibraryFragment;
import com.cloudsigns.factorymanager2.engineering.fragment.EngineeringToolUseFragment;
import com.cloudsigns.factorymanager2.project.adapter.ProjectMainViewPageAdapter;
import com.cloudsigns.factorymanager2.project.customview.CustomViewPager;
import com.cloudsigns.factorymanager2.project.customview.PageIndicatorView;

import java.util.ArrayList;

public class EngineeringUseFragmentActivity extends FragmentActivity implements View.OnClickListener{
    private ImageView img_back;
    private LinearLayout ll_right;
    private RelativeLayout rl_material_use,rl_tool_use,rl_outfield_purchase,rl_return_library;
    private TextView tv_material_use,tv_tool_use,tv_outfield_purchase,tv_return_library;
    private CustomViewPager viewpager;

    private ArrayList<Fragment> listFragments;
    private ProjectMainViewPageAdapter adapter;
    public static final int MATERIAL_USE = 0;
    public static final int TOOL_USE = 1;
    public static final int OUTFIELD_PURCHASE = 2;
    public static final int RETURN_LIBRARY = 3;
    public int currentIndex = MATERIAL_USE;


    private EngineeringMaterialUseFragment engineeringMaterialUseFragment;
    private EngineeringToolUseFragment engineeringToolUseFragment;
    private EngineeringOutfieldPurchaseFragment engineeringOutfieldPurchaseFragment;
    private EngineeringReturnLibraryFragment engineeringReturnLibraryFragment;


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);//去掉标题栏
        // 沉浸式状态栏
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            Window window = getWindow();
            window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
            window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
            window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
            window.setStatusBarColor(Color.TRANSPARENT);
        } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
        }
        setContentView(R.layout.layout_engineering_use);

            init();
    }

    private void init(){
        img_back=findViewById(R.id.img_back);
        ll_right=findViewById(R.id.ll_right);
        rl_material_use=findViewById(R.id.rl_material_use);
        rl_tool_use=findViewById(R.id.rl_tool_use);
        rl_outfield_purchase=findViewById(R.id.rl_outfield_purchase);
        rl_return_library=findViewById(R.id.rl_return_library);
        tv_material_use=findViewById(R.id.tv_material_use);
        tv_tool_use=findViewById(R.id.tv_tool_use);
        tv_outfield_purchase=findViewById(R.id.tv_outfield_purchase);
        tv_return_library=findViewById(R.id.tv_return_library);
        viewpager = (CustomViewPager) findViewById(R.id.viewpager);

        img_back.setOnClickListener(this);
        ll_right.setOnClickListener(this);
        rl_material_use.setOnClickListener(this);
        rl_tool_use.setOnClickListener(this);
        rl_outfield_purchase.setOnClickListener(this);
        rl_return_library.setOnClickListener(this);

        engineeringMaterialUseFragment=new EngineeringMaterialUseFragment();
        engineeringToolUseFragment=new EngineeringToolUseFragment();
        engineeringOutfieldPurchaseFragment=new EngineeringOutfieldPurchaseFragment();
        engineeringReturnLibraryFragment=new EngineeringReturnLibraryFragment();

        listFragments = new ArrayList<Fragment>();
        listFragments.add(engineeringMaterialUseFragment);
        listFragments.add(engineeringToolUseFragment);
        listFragments.add(engineeringOutfieldPurchaseFragment);
        listFragments.add(engineeringReturnLibraryFragment);


        adapter = new ProjectMainViewPageAdapter(getSupportFragmentManager(), listFragments);
        viewpager.setAdapter(adapter);
        viewpager.setOffscreenPageLimit(4);

        PageIndicatorView indicator = (PageIndicatorView) findViewById(R.id.indicator);
        indicator.setViewPager(viewpager);
        indicator.setFades(false);
        indicator.setSelectedColor(getResources().getColor(R.color.color_blue_17a5ef));
        indicator.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {

            @Override
            public void onPageSelected(int position) {
                showSelectedOption(position);

            }

            @Override
            public void onPageScrolled(int arg0, float arg1, int arg2) {
            }

            @Override
            public void onPageScrollStateChanged(int arg0) {

            }
        });

    }

    /**
     * 切换到哪个tab页
     *
     * @param pos
     */
    private void showSelectedOption(int pos) {
        // TODO Auto-generated method stub
        switch (pos) {
            case MATERIAL_USE:
                rl_material_use.setSelected(true);
                rl_tool_use.setSelected(false);
                rl_outfield_purchase.setSelected(false);
                rl_return_library.setSelected(false);

                tv_material_use.setTextColor(getResources().getColor(R.color.color_blue_17a5ef));
                tv_tool_use.setTextColor(getResources().getColor(R.color.color_bbbbbb));
                tv_outfield_purchase.setTextColor(getResources().getColor(R.color.color_bbbbbb));
                tv_return_library.setTextColor(getResources().getColor(R.color.color_bbbbbb));
                break;

            case TOOL_USE:
                rl_material_use.setSelected(false);
                rl_tool_use.setSelected(true);
                rl_outfield_purchase.setSelected(false);
                rl_return_library.setSelected(false);

                tv_material_use.setTextColor(getResources().getColor(R.color.color_bbbbbb));
                tv_tool_use.setTextColor(getResources().getColor(R.color.color_blue_17a5ef));
                tv_outfield_purchase.setTextColor(getResources().getColor(R.color.color_bbbbbb));
                tv_return_library.setTextColor(getResources().getColor(R.color.color_bbbbbb));
                break;

            case OUTFIELD_PURCHASE:
                rl_material_use.setSelected(false);
                rl_tool_use.setSelected(false);
                rl_outfield_purchase.setSelected(true);
                rl_return_library.setSelected(false);

                tv_material_use.setTextColor(getResources().getColor(R.color.color_bbbbbb));
                tv_tool_use.setTextColor(getResources().getColor(R.color.color_bbbbbb));
                tv_outfield_purchase.setTextColor(getResources().getColor(R.color.color_blue_17a5ef));
                tv_return_library.setTextColor(getResources().getColor(R.color.color_bbbbbb));
                break;

            case RETURN_LIBRARY:
                rl_material_use.setSelected(false);
                rl_tool_use.setSelected(false);
                rl_outfield_purchase.setSelected(false);
                rl_return_library.setSelected(true);

                tv_material_use.setTextColor(getResources().getColor(R.color.color_bbbbbb));
                tv_tool_use.setTextColor(getResources().getColor(R.color.color_bbbbbb));
                tv_outfield_purchase.setTextColor(getResources().getColor(R.color.color_bbbbbb));
                tv_return_library.setTextColor(getResources().getColor(R.color.color_blue_17a5ef));
                break;

            default:
                break;
        }
    }

    @Override
    public void onClick(View view) {
        Intent mIntent;
        switch (view.getId()){
            case R.id.img_back:
                finish();
                break;
            case R.id.ll_right:
                mIntent=new Intent(EngineeringUseFragmentActivity.this,EngineeringUseAddActivity.class);
                startActivity(mIntent);
                break;
                // 材料领用
            case R.id.rl_material_use:
                if (currentIndex != MATERIAL_USE) {
                    currentIndex = MATERIAL_USE;
                    viewpager.setCurrentItem(MATERIAL_USE);
                    showSelectedOption(MATERIAL_USE);
                }
                break;
                // 工具领用
            case R.id.rl_tool_use:
                if (currentIndex != TOOL_USE) {
                    currentIndex = TOOL_USE;
                    viewpager.setCurrentItem(TOOL_USE);
                    showSelectedOption(TOOL_USE);
                }
                break;
                // 外场采购
            case R.id.rl_outfield_purchase:
                if (currentIndex != OUTFIELD_PURCHASE) {
                    currentIndex = OUTFIELD_PURCHASE;
                    viewpager.setCurrentItem(OUTFIELD_PURCHASE);
                    showSelectedOption(OUTFIELD_PURCHASE);
                }
                break;
                // 工程退库
            case R.id.rl_return_library:
                if (currentIndex != RETURN_LIBRARY) {
                    currentIndex = RETURN_LIBRARY;
                    viewpager.setCurrentItem(RETURN_LIBRARY);
                    showSelectedOption(RETURN_LIBRARY);
                }
                break;
        }
    }
}
